package com.cap.bts.crmdp.dealer.remind.scheduler;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.cap.bts.crmdp.commons.sftp.SFTPProcessor;
import com.cap.bts.crmdp.commons.sftp.SftpProperties;
import com.cap.bts.crmdp.dealer.dealerinfosync.util.ProcessCsvFileUtils;
import com.cap.bts.crmdp.dealer.purchase.constants.PurchaseConstants;
import com.cap.bts.crmdp.dealer.remind.dto.SftpFileDto;
import com.cap.bts.crmdp.dealer.remind.service.IRemindService;
import com.cap.bts.crmdp.dealer.remind.service.impl.RemindServiceImpl;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.ApplicationContextUtil;
import com.cap.bts.framework.scheduler.service.BaseJobBean;
import com.jcraft.jsch.ChannelSftp;

public class ServiceRemindFileSynchronizationJob extends BaseJobBean {
	private static final Logger LOGGER = Logger.getLogger(RemindServiceImpl.class);
	@Override
	public void work() throws Exception {
		processServiceRemind();
	}
	
	public void processServiceRemind() throws AppException{
		//SFTP取出数据
		try {
			//从SFTP上下载文件
			IRemindService remindService = (IRemindService) ApplicationContextUtil.getBean("remindServiceImpl");
			ICodeMapping codeMappingService = (ICodeMapping) ApplicationContextUtil.getBean("codeMappingImpl");
			String sftpInfo = codeMappingService.getCodeValueByCode(PurchaseConstants.SFTP_INFO, PurchaseConstants.SERVICE_REMIND_FILE_SYNCHRONIZATION);
			try {
				if(sftpInfo != null){
					SftpProperties sftpProperties = JSON.parseObject(sftpInfo, SftpProperties.class);
					//取出所有的文件
					String prefix = codeMappingService.getCodeValueByCode("SRFromWeCareOP", "SRFromWeCareOP");
					List<SftpFileDto> fileList = remindService.getAllFileOrderByDateAsc(sftpProperties, prefix);
					SFTPProcessor ftpUtil = new SFTPProcessor();
					ChannelSftp channelSftp = ftpUtil.getConnect(sftpProperties);
					
					if(channelSftp.isConnected()){
						//获取临时目录
						String tmpPath = codeMappingService.getCodeValueByCode("tmpPath", "tmpPath");
						
						for(int j=fileList.size()-1; j>=0; j--){
							try{
								SftpFileDto sftpFileDto = fileList.get(j);
								String localFilePath = tmpPath + "ServiceReminder/ServiceReminder_" + DateFormatUtils.format(new Date(), "yyyyMMdd_HHmmssSSS");
								String localFile = localFilePath + "/" + sftpFileDto.getFileName();
								File file = new File(localFilePath);
								if(!file.exists()){
									file.mkdirs();
								}
								ftpUtil.downloadFile(sftpFileDto.getFileName(), sftpProperties.getPath(), localFile);
								//解压缩
								ProcessCsvFileUtils.unZip(localFile, localFilePath);
								String localUnZipFile = localFilePath + "/MaintenanceStatus.csv";
								remindService.importServiceRemindDataFromFile(new File(localUnZipFile), sftpFileDto.getFileName() + (int)(Math.random() * (99999 - 10000 + 1)) + 10000);
								
								//将处理过的压缩包删除
								ftpUtil.remove(sftpProperties.getPath() + sftpFileDto.getFileName());		
								}catch(Exception e){
									LOGGER.error("processServiceRemindFileError",e);
								}
						}
						
					}
					ftpUtil.closeChannel();
				}
			} catch (RuntimeException e) {
				LOGGER.info("downLodeServiceRemindFile", e);
			} catch (Exception e) {
				LOGGER.info("downLodeServiceRemindFile", e);
			}
		} catch (AppException e) {
			throw new AppException("Exception processServiceRemindFileSynchronization", e);
		}
	}
}
